package com.shopnight.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shopnight.entity.ShopAddress;
import com.shopnight.interceptors.JWTInterceptor;
import com.shopnight.mapper.ShopAddressMapper;
import com.shopnight.result.ApiResult;
import com.shopnight.result.ApiResultList;
import com.shopnight.service.AddressOptionService;
import com.shopnight.service.ShopAddressService;
import com.shopnight.utils.UserContext;
import com.shopnight.vo.AddressOptionVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;



@RestController
@RequestMapping("/api/token")
@Api(tags = {"AddressController"},description = "地址业务所需接口")
public class AddressController {
    @Autowired
    ShopAddressService shopAddressService;

    @Autowired
    ShopAddressMapper shopAddressMapper;

    @Autowired
    AddressOptionService addressOptionService;


    @PostMapping("/addOrUpdateAddress")
    @ApiOperation(value = "添加地址或更改地址接口 当携带地址id则为更改 当无地址id时则为添加",notes = "需传（地址id），用户id,楼栋,楼层,室号,是否选中,收货人，收货人电话号码，详细地址")
    public  Object addAddress(@ApiParam("参数为(id) userId oneSite twoSite threeSite") @RequestBody ShopAddress shopAddress){
        if ( shopAddress == null) return  ApiResult.fail("请提交数据！");

        if (shopAddress.getId() != null){
            //Integer checked = shopAddressService.selectAddress(shopAddress.getId().intValue()).getChecked();
            if(shopAddress.getChecked() == 1){
                //更新用户其他地址 为未选中
                shopAddressMapper.updateChecked( shopAddress.getUserId() );
            }
        }

        //对于保存数据其实不需要如此判断 可以尝试缩减成一行
        if( shopAddressService.saveOrUpdate(shopAddress) ){
            return ApiResult.ok("操作成功！");
        }else {
            return ApiResult.fail("操作失败！");
        }
    }

    @GetMapping("/getAddressByUserId")
    @ApiOperation(value = "获取用户的地址",notes = "无需传参")
    public  ApiResultList getAddressByUserId( ){
        String userId =  UserContext.getEndUserId();

        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id",userId);
        return ApiResultList.ok(shopAddressService.list(queryWrapper));
    }



    @GetMapping("/getAddressAll")
    @ApiOperation(value = "获取所有可选地址",notes = "需传组织id")
    public  ApiResult getAddressAll(@RequestParam("orgId") Integer orgId){

        AddressOptionVo addressOptionVo = addressOptionService.getAddressAll(orgId);
        return ApiResult.ok(addressOptionVo);
    }


    @GetMapping("/delAddress")
    @ApiOperation(value = "删除用户地址",notes = "需传地址id")
    public  ApiResult delAddressAll(@RequestParam("addressId") Integer addressId){
        String userId =  UserContext.getEndUserId();
        QueryWrapper<ShopAddress> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId).eq("id",addressId);
        return ApiResult.ok(shopAddressMapper.delete(queryWrapper));
    }

}
